package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.mapper.SetmealDishMapper;
import com.itheima.pojo.SetmealDish;
import com.itheima.pojo.SetmealNew;
import com.itheima.service.SetmealDishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 放学后海堤日记
 * @Date: 2023/4/14 20:38
 * @Desc:
 */
@Service
public class SetmealDishServiceImpl implements SetmealDishService {
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addSetmealDish(SetmealNew setmealNew) {
        List<SetmealDish> setmealDishes = setmealNew.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealNew.getId());
            int i = setmealDishMapper.insert(setmealDish);

            if (i == 0)
                i = 1 / 0;
        }

        return 1;
    }

    @Override
    public boolean findSetmeatlDishByDishId(Long[] ids) {
        for (Long id : ids) {
            QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
            wrapper.eq("dish_id", id);

            List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper);

            if (setmealDishes.size() != 0)
                return false;
        }
        return true;
    }

    @Override
    public List<SetmealDish> findSetmeatlDishByDishId(Long id) {
        QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
        wrapper.eq("dish_id", id);

        return setmealDishMapper.selectList(wrapper);
    }

    @Override
    public int deleteBySetmealId(Long id) {
        QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id", id);

        return setmealDishMapper.delete(wrapper);
    }

    @Override
    public List<SetmealDish> findBySetmealId(Long id) {
        QueryWrapper<SetmealDish> wrapper = new QueryWrapper<>();
        wrapper.eq("setmeal_id", id);

        return setmealDishMapper.selectList(wrapper);
    }
}
